package com.leetcode.周赛.第233场;

import java.util.ArrayDeque;

/**
 * @author: xiaomi
 * @date: 2021/3/21
 * @description: 5709. 最大升序子数组和
 * https://leetcode-cn.com/contest/weekly-contest-233/problems/maximum-ascending-subarray-sum/
 */
public class A_5709_最大升序子数组和 {

    /**
     * 单调栈
     *
     * @param nums
     * @return
     */
    public int maxAscendingSum(int[] nums) {
        int len = nums.length;
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        int max = 0;
        for (int i = 0; i < len; i++) {
            if (deque.isEmpty() || nums[i] > deque.peekLast()) {
                //符合升序
                //deque.addLast(nums[i]);
            } else {
                int sum = 0;
                while (!deque.isEmpty()) {
                    sum += deque.removeLast();
                }
                max = Math.max(max, sum);
            }
            deque.addLast(nums[i]);
        }
        if (!deque.isEmpty()){
            int sum = 0;
            while (!deque.isEmpty()) {
                sum += deque.removeLast();
            }
            max = Math.max(max, sum);
        }
        return max;
    }
}
